﻿using System;
using System.Collections;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Collections.Generic;
using log4net;

namespace BidFunLib
{

    public class BidSpyConfigHelper
    {

        private static readonly ILog log = LogManager.GetLogger(typeof(BidSpyConfigHelper));

        private static BidSpyConfigHelper instance;

        public static BidSpyConfigHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new BidSpyConfigHelper();
                }
                return BidSpyConfigHelper.instance;
            }
            set
            {
                BidSpyConfigHelper.instance = value;
            }
        }

        private System.Configuration.Configuration Config;

        private BidSpyConfig Observerconfig;

        private BidSpyConfigHelper()
        {
            Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) as Configuration;
            if (Config == null)
            {
                log.Error("BidSpyConfigHelper : Failed to load BidSales in App.Config");
            }
            else
            {
                log.Info("BidSpyConfigHelper : Configuration loaded");
                Observerconfig = ConfigurationManager.GetSection("BidSales") as BidSpyConfig;
            }
        }

        public List<BidObjectSpy> getList()
        {
            List<BidObjectSpy> list = new List<BidObjectSpy>();
            if (Observerconfig != null)
            {
                for (int i = 1; i < Observerconfig.BidSales.Count; i++)
                {
                    list.Add(Observerconfig.BidSales[i]);
                }
            }
            return list;
        }

        public bool getAllSales()
        {
            return Observerconfig.Allsales;
        }
    }

    /// <summary>
    /// Configuration BidList Section
    /// </summary>
    public class BidSpyConfig : ConfigurationSection
    {
        [ConfigurationProperty("BidObjectSpyList", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(BidObjectSpyCollection),
            AddItemName = "add",
            ClearItemsName = "clear",
            RemoveItemName = "remove")]
        public BidObjectSpyCollection BidSales
        {
            get
            {
                BidObjectSpyCollection collection = (BidObjectSpyCollection)base["BidObjectSpyList"];
                return collection;
            }
        }

        [ConfigurationProperty("allSales", IsRequired = true, IsKey = true, DefaultValue = "false")]
        public Boolean Allsales
        {
            get
            {
                return (Boolean)this["allSales"];
            }
            set
            {
                this["allSales"] = value;
            }
        }
    }



    public class BidObjectSpyCollection : ConfigurationElementCollection
    {
        public BidObjectSpyCollection()
        {
            BidObjectSpy elem = (BidObjectSpy)CreateNewElement();
            Add(elem);
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new BidObjectSpy();
        }

        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((BidObjectSpy)element).Name;
        }

        public BidObjectSpy this[int index]
        {
            get
            {
                return (BidObjectSpy)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        new public BidObjectSpy this[string Name]
        {
            get
            {
                return (BidObjectSpy)BaseGet(Name);
            }
        }

        public int IndexOf(BidObjectSpy bidSaleName)
        {
            return BaseIndexOf(bidSaleName);
        }

        public void Add(BidObjectSpy bidSaleName)
        {
            BaseAdd(bidSaleName);
        }
        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
        }

        public void Remove(BidObjectSpy bidSaleName)
        {
            if (BaseIndexOf(bidSaleName) >= 0)
                BaseRemove(bidSaleName.Name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Clear()
        {
            BaseClear();
        }

    }

    /// <summary>
    /// Bid Sale Configuration Element
    /// </summary>
    public class BidObjectSpy : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("spy", IsRequired = false, IsKey = false, DefaultValue = "false")]
        public Boolean Spy
        {
            get
            {
                return (Boolean)this["spy"];
            }
            set
            {
                this["spy"] = value;
            }
        }

        public BidObjectSpy(String name)
        {
            this.Name = name;

        }

        public BidObjectSpy()
        {
            this.Name = "";
        }


    }




}
